
#include	"SysConfig.h"
#include	"Define.h"
#include	"Function.h"

#include	"Bluetooth.h"
#include	"Display.h"
#include	"UI_Main.h"
#include	"PhoneBook.h"
#include	"Utill.h"
#include	"Model_Option.h"

#ifdef  HERO
#include	"Hero_Drv.h"
#endif
#ifdef  MOVON
#include	"Movon.h"
#endif
#include	"Port.h"

#ifdef  BLUETOOTH

unsigned char ucBT_MIC_Volume;
unsigned char ucBT_UserReject;
unsigned char ucBT_InitOK;
unsigned char ucBT_AutoConnection;
unsigned char ucBT_Function_Active;
unsigned char ucBT_InitCold;
unsigned char ucBT_DeviceCnt;
unsigned char ucBT_Volume;
#define	BT_CMD_CNT		10
unsigned char ucBT_CmdBuf[BT_CMD_CNT];
unsigned char ucBT_Cmd_wPtr;
unsigned char ucBT_Cmd_rPtr;
unsigned char ucBT_ProcessSeq;
unsigned char ucBT_ConnectRequestIndex;
unsigned char ucBT_DeleteRequestIndex;
unsigned char ucBT_PairedDeviceIndex;
unsigned char ucBT_LocalName[BT_LOCALNAME_SIZE];
unsigned char ucBT_LocalAddress[6];
unsigned char ucBT_DialNumber[PBAP_NUMBER_BUFF_SIZE];
unsigned char ucBT_DialNumberCnt;
unsigned char ucBT_CallerID[BT_CALLERID_LEN];
unsigned short usBT_AutoConnect_Timer100msec;
unsigned short usBT_ProcessSeq_Timer50msec;
unsigned short usBT_Pair_Timer50msec;
unsigned short usBT_Status;
unsigned char ucBT_RootCall;
unsigned char ucBT_PBAP_Download_Timer50msec;
unsigned char ucBT_PBAP_Download_Active;
unsigned char ucBT_DB_Address;

BT_AVRCP ucBT_AVRCP_Cmd;
BT_DEVICE	sBT_BDList[BT_DEVICE_NUM];
BT_DEVICE	sBT_ConnectedDevice;
BT_DEVICE	sUI_BT_DeviceList_Delete;
BT_PROC ucBT_Process;

void BT_BluetoothVolume(unsigned char ucDes, unsigned char ucOption);
void BT_BluetoothVolumeInit(void);
void BT_PBAP_SetDownload(unsigned char ucDes);
void BT_BluetoothOnOff(unsigned char ucDes);
unsigned char BT_ConnectRequest(unsigned char ucOrder);
void BT_PairingRequest(void);
void BT_DisconnectRequest(void);
void BT_DeleteRequest(unsigned char ucOrder);
void BT_DialNumberRequest(void);
void BT_AVRCP_Command(BT_AVRCP ucCmd);
unsigned char BT_PairingCancel(void);
extern MV_HF_AUDIO ucMV_HF_AudioConnect;	// 0 : Phone, 1 : Audio


#ifdef  MOVON

extern unsigned char ucMV_BootOn;
extern unsigned char ucMV_BTOnOff;
extern unsigned char ucMV_InitOk;
extern unsigned char ucMV_PairingStatus;
extern unsigned char ucMV_AV_DisconnectStatus;
extern unsigned char ucMV_HF_DisconnectStatus;
extern MV_HF_STATUS ucMV_HFS_Status;
extern MV_AV_STATUS ucMV_A2DP_Status;
extern MV_AVRCP_STATUS ucMV_AVRCP_Status;
extern MV_PB_STATUS ucMV_PBAP_Status;
extern MV_HF_PROFILE ucMV_HF_Profile;

extern void MV_TX_HF_NumberDial(unsigned char *ucNum, unsigned char ucCnt);
#endif

extern void MENU_SetID(UI_MENU_ID ucID);
extern unsigned char MENU_GetID(void);

#ifdef  PHONE_BOOK
extern unsigned char ucPBAP_CallHistoryCnt;
extern void PBAP_DataInit(void);
extern void PBAP_SelectMode(unsigned char ucMode);
extern unsigned char ucPBAP_CurrentContents;
extern void PBAP_InitBlockData(void);
#endif
extern void AUDIO_SetMute(unsigned char ucDes);

extern void DISP_SetTempDisplayMode_6Sec(UI_DISP ucDes);

#ifdef  SERIAL_FLASH
extern void SF_WriteBluetoothDeviceAddress(void);
extern void SF_ErasePhoneData_Request(void);
#endif

extern unsigned char ucMENU_CurNo;
extern PBAP_LIST	sPBAP_CallHistory[PBAP_NUMBER_CALL_HISTORY];
extern unsigned char Bluetooth_DISP_CallNumberSize(unsigned char *ucNum, unsigned char ucSize);

extern void MV_TX_BootMode(unsigned char ucMode);
extern void MV_PB_SkipInit(void);

void BT_SetDBAddress(unsigned char ucMode)
{
	ucBT_DB_Address = ucMode;
}

unsigned char BT_GetDBAddress(void)
{
	return(ucBT_DB_Address);
}

unsigned char BT_NumberDial_CallHistory(void)
{
	unsigned char ucRet;
	
	if	(Bluetooth_DISP_CallNumberSize(&sPBAP_CallHistory[ucMENU_CurNo].ucHome[0], PBAP_NUMBER_BUFF_SIZE) != 0)
	{
		memcpy(&ucBT_DialNumber[0], &sPBAP_CallHistory[ucMENU_CurNo].ucHome[0], PBAP_NUMBER_BUFF_SIZE);
		ucBT_DialNumberCnt = PBAP_NUMBER_BUFF_SIZE;
		BT_DialNumberRequest();
		ucRet = 1;
	}
	else if  (Bluetooth_DISP_CallNumberSize(&sPBAP_CallHistory[ucMENU_CurNo].ucWork[0], PBAP_NUMBER_BUFF_SIZE) != 0)
	{
		memcpy(&ucBT_DialNumber[0], &sPBAP_CallHistory[ucMENU_CurNo].ucWork[0], PBAP_NUMBER_BUFF_SIZE);
		ucBT_DialNumberCnt = PBAP_NUMBER_BUFF_SIZE;
		BT_DialNumberRequest();
		ucRet = 1;
	}
	else if  (Bluetooth_DISP_CallNumberSize(&sPBAP_CallHistory[ucMENU_CurNo].ucCell[0], PBAP_NUMBER_BUFF_SIZE) != 0)
	{
		memcpy(&ucBT_DialNumber[0], &sPBAP_CallHistory[ucMENU_CurNo].ucCell[0], PBAP_NUMBER_BUFF_SIZE);
		ucBT_DialNumberCnt = PBAP_NUMBER_BUFF_SIZE;
		BT_DialNumberRequest();
		ucRet = 1;
	}
	else if  (Bluetooth_DISP_CallNumberSize(&sPBAP_CallHistory[ucMENU_CurNo].ucOther[0], PBAP_NUMBER_BUFF_SIZE) != 0)
	{
		memcpy(&ucBT_DialNumber[0], &sPBAP_CallHistory[ucMENU_CurNo].ucOther[0], PBAP_NUMBER_BUFF_SIZE);
		ucBT_DialNumberCnt = PBAP_NUMBER_BUFF_SIZE;
		BT_DialNumberRequest();
		ucRet = 1;
	} 
	else
	{
	}
	return(ucRet);
}

unsigned char BT_ConnectStatus(void)
{
	if  (ucMV_HFS_Status > MV_HFS_CONNECTABLE)
	{
		return(1);
	}

	if  (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
	{
		return(1);
	}
	return(0);
}

unsigned char BT_HF_ConnectStatus(void)
{
	if  (ucMV_HFS_Status > MV_HFS_CONNECTABLE)
	{
		return(1);
	}

	return(0);
}

unsigned char BT_HF_CallStatus(void)
{
	if  (ucMV_HFS_Status > MV_HFS_CONNECTED)
	{
		return(1);
	}

	return(0);
}

unsigned char BT_A2DP_ConnectStatus(void)
{
	if  (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
	{
		return(1);
	}
	return(0);
}

unsigned char BT_GetBDNumberByAddress(unsigned char *ucAdrs)
{
	unsigned char i;

	for(i = 0; i < ucBT_DeviceCnt; ++i)
	{
		if  (memcmp(&sBT_BDList[i].ucDeviceAdr, ucAdrs, 6) == 0)
		{
			break;
		}
	}

	return(i);
}

unsigned char BT_GetBDIndexByOrder(unsigned char ucOrder)
{
	unsigned char i;
	
	for(i = 0; i < ucBT_DeviceCnt; ++i)
	{
		if  (sBT_BDList[i].ucOrder == ucOrder)
		{
			break;
		}
	}

	return(i);
}

/*///////////////////////////////////////////////////////////////////////////////
Title : void BT_WriteBD(unsigned char ucMode,unsigned char *ucBuf)
ucMode : 
	0x00 : All ( Name, Address, Service )
	0x01 : Name
	0x02 : Address
	0x03 : Service
///////////////////////////////////////////////////////////////////////////////*/
void BT_WriteBD(unsigned char ucMode,unsigned char *ucBuf, unsigned char ucLen)
{
	unsigned char ucNum;
	
	switch  (ucMode)
	{
		case 0x00:
			break;
		case 0x01:
			ucNum = BT_GetBDNumberByAddress(ucBuf);
			memset(&sBT_BDList[ucNum].ucName[0], 0x00, BT_DEVICE_NAME_LENGTH);
			UTILL_UTF8ToUnicode(ucBuf + 7, &sBT_BDList[ucNum].ucName[0], ucLen, BT_DEVICE_NAME_LENGTH);
			break;
		case 0x02:
			break;
		case 0x03:
			break;
	}
}

void BT_Update_BDSorting(unsigned char ucIndex)
{
	unsigned char i;
	unsigned char ucOrderNum;

	ucOrderNum = sBT_BDList[ucIndex].ucOrder;
	sBT_BDList[ucIndex].ucOrder = 0;

	for(i = 0; i < BT_DEVICE_NUM; ++i)
	{
		if  (ucIndex == i)
		{
		}
		else
		{
			if  (sBT_BDList[i].ucOrder < ucOrderNum)
			{
				++sBT_BDList[i].ucOrder;
			}
		}
	}
}

void BT_BluetoothVariableInitialize(void)
{
	if  (OPT_GetOption(OPT_MASK_BT) == 0)
	{
		return;
	}
	ucBT_Volume = BT_VOL_DEFAULT;
	ucBT_Function_Active = 1;
	BT_SetDBAddress(BT_DB_ADR_NORMAL);
}

void BT_SetProcess(unsigned char ucMode)
{
	if  (ucBT_InitOK == 0)
	{
		return;
	}
	if  (ucBT_Process == ucMode)
	{
		return;
	}

	if  (ucBT_Process == BT_PRS_PAIRING)
	{
		BT_PairingCancel();
	}
	
	LOG_BT("BT_SetProcess:%d",ucMode);
	ucBT_CmdBuf[ucBT_Cmd_wPtr] = ucMode;
	++ucBT_Cmd_wPtr;
	ucBT_Cmd_wPtr = ucBT_Cmd_wPtr % BT_CMD_CNT;
}

void BT_ProcessInit(void)
{
	ucBT_Cmd_wPtr = 0;
	ucBT_Cmd_rPtr = 0;
	ucBT_Process = 0;
	ucBT_ProcessSeq = 0;
}

unsigned char BT_GetProcess(void)
{
	unsigned char ucData;

	ucData = ucBT_CmdBuf[ucBT_Cmd_rPtr];
	++ucBT_Cmd_rPtr;
	ucBT_Cmd_rPtr = ucBT_Cmd_rPtr % BT_CMD_CNT;
	
	return(ucData);
}

void BT_PairProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{
		case BT_PAIRING_PRECHECK:
			if  (
				(ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY)
				)
			{
				if  (ucMV_PBAP_Status == MV_PBAP_DOWNLOADING)
				{
					MV_TX_PhoneBookAbort();
				}
				MV_TX_DisconnectAll();
				usBT_ProcessSeq_Timer50msec = 10000/50;
				ucBT_ProcessSeq = BT_PAIRING_WAIT_DISCON;
				LOG_BT("BT_PAIRING_PRECHECK:Discon");
			}
			else
			{
				usBT_ProcessSeq_Timer50msec = 1000/50;
				ucBT_ProcessSeq = BT_PAIRING_SET;
				LOG_BT("BT_PAIRING_PRECHECK");
			}
			break;
		case BT_PAIRING_WAIT_DISCON:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY))
			{
				if  (usBT_ProcessSeq_Timer50msec == 0)
				{
					// Fail.....
					BT_PairingCancel();
					LOG_BT("BT_PAIRING_WAIT_DISCON:Fail");
				}
			}
			else
			{
				ucBT_ProcessSeq = BT_PAIRING_SET;
				LOG_BT("BT_PAIRING_WAIT_DISCON");
			}
			break;
		case BT_PAIRING_SET:			
			if  (ucMV_PairingStatus == 0x01)
			{
				ucBT_ProcessSeq = BT_PAIRING_WAIT;
				LOG_BT("BT_PAIRING_SET:BT_PAIRING_WAIT");
			}
			else
			{
				if  (usBT_ProcessSeq_Timer50msec == 0)
				{
					usBT_ProcessSeq_Timer50msec = 5000/50;
					LOG_BT("BT_PAIRING_SET:0");
					MV_TX_StartPairing();
				}
			}
			break;
		case BT_PAIRING_WAIT:
			if  (usBT_Pair_Timer50msec == 0)
			{
				BT_PairingCancel();
				DISP_SetTempDisplayMode_6Sec(DISP_BT_PAIR_NO_DEVICE);
			}
			else
			{
				if  (
					(ucMV_HFS_Status > MV_HFS_CONNECTABLE)
					|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
					)
				{
					if  (MENU_GetID() == MENU_BT_PAIR_ACTIVE_ID)
					{
						MENU_SetID(MENU_NONE);
					}
					ucBT_Process = BT_PRS_NONE;
					usBT_Pair_Timer50msec = 0x00;
					DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
				}
			}
			break;
		case BT_PAIRING_SUCCESS:
			BT_PairingCancel();
			DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
			LOG_BT("BT_PAIRING_SUCCESS");
			break;
		case BT_PAIRING_READ_DEVICE_INFO:
			LOG_BT("BT_PAIRING_READ_DEVICE_INFO");
			MV_TX_ReadRemoteNameToBDAddr(sBT_BDList[ucBT_PairedDeviceIndex]);
			BT_PairingCancel();
			LOG_BT("BT_PAIRING_REQ_CONNECT");
			break;
		case BT_PAIRING_FAIL:
			BT_PairingCancel();
			DISP_SetTempDisplayMode_6Sec(DISP_BT_PAIR_NO_DEVICE);
			break;
	}
}

void BT_DeleteProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{
		case BT_DELETE_PRE:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY))
			{
				if  (ucMV_PBAP_Status == MV_PBAP_DOWNLOADING)
				{
					MV_TX_PhoneBookAbort();
				}
				MV_TX_DisconnectAll();
				usBT_ProcessSeq_Timer50msec = 10000/50;
				ucBT_ProcessSeq = BT_DELETE_WAIT_DISCON;
			}
			else
			{
				ucBT_ProcessSeq = BT_DELETE_SET_DELETE;
				usBT_ProcessSeq_Timer50msec = 5000/50;
			}
			break;
		case BT_DELETE_WAIT_DISCON:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY))
			{
				if  (usBT_ProcessSeq_Timer50msec == 0)
				{
					ucBT_Process = BT_PRS_NONE;// Fail
				}
			}
			else
			{
				ucBT_ProcessSeq = BT_DELETE_SET_DELETE;
				usBT_ProcessSeq_Timer50msec = 5000/50;
			}
			break;
		case BT_DELETE_SET_DELETE:
			ucBT_ProcessSeq = BT_DELETE_WAIT_DELETE;
			MV_TX_ClearPairedDevice(sBT_BDList[ucBT_DeleteRequestIndex]);
			break;
		case BT_DELETE_WAIT_DELETE:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_Process = BT_PRS_NONE;
			}
			break;
		case BT_DELETE_REQ_LIST:
			MV_TX_ReadPairedDevice();
			ucBT_Process = BT_PRS_NONE;
			ucBT_ProcessSeq = 0;
			break;
	}
}

unsigned char BT_GetInitStatus(void)
{
	return(ucBT_InitOK);
}

void BT_SetInitStart(void)
{
	ucBT_ProcessSeq = BT_INIT_SEQ_RESET0;
}

void BT_InitializeProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{
		case BT_INIT_SEQ_RESET0:
			PO_MOVON_RST = 0;			
			BT_ProcessInit();
			usBT_ProcessSeq_Timer50msec = 500/50;
			ucBT_ProcessSeq = BT_INIT_SEQ_RESET1;
			LOG_BT("BT_INIT_SEQ_RESET0");
			break;
		case BT_INIT_SEQ_RESET1:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				PO_MOVON_RST = 1;
				ucBT_ProcessSeq = BT_INIT_SEQ_WAIT;
				LOG_BT("BT_INIT_SEQ_RESET1");
			}
			break;
		case BT_INIT_SEQ_WAIT:
			if  (ucMV_InitOk == 1)
			{
	//			MV_TX_SYS_FW_Version();
				ucBT_ProcessSeq = BT_INIT_SEQ_CMD1;
				usBT_ProcessSeq_Timer50msec = 50/50;
				LOG_BT("BT_INIT_SEQ_WAIT");
			}
			break;
		case BT_INIT_SEQ_CMD1:			
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				LOG_BT("BT_INIT_SEQ_CMD1");
				MV_TX_BTOn();
				usBT_ProcessSeq_Timer50msec = 100/50;
				ucBT_ProcessSeq = BT_INIT_SEQ_CMD2;
				switch  (BT_GetDBAddress())
				{
					case BT_DB_ADR_NORMAL:
						break;
					case BT_DB_ADR_01:
					case BT_DB_ADR_02:
						usBT_ProcessSeq_Timer50msec = 0;
						ucBT_ProcessSeq = BT_INIT_SEQ_BOOT_WAIT;
						break;
				}
			}
			break;
		case BT_INIT_SEQ_BOOT_WAIT:
			if  (ucMV_BootOn == 0)
			{
				return;
			}
			usBT_ProcessSeq_Timer50msec = 100/50;
			ucBT_ProcessSeq = BT_INIT_SEQ_WAIT;
			ucMV_InitOk = 0;
			switch  (BT_GetDBAddress())
			{
				case BT_DB_ADR_NORMAL:
					break;
				case BT_DB_ADR_01:
					MV_TX_BootMode(0x01);
					BT_SetDBAddress(BT_DB_ADR_NORMAL);
		//			BT_SetProcess(BT_PRS_LOOPBACK);
					break;
				case BT_DB_ADR_02:
					MV_TX_BootMode(0x02);
					BT_SetDBAddress(BT_DB_ADR_NORMAL);
		//			BT_SetProcess(BT_PRS_LOOPBACK);
					break;
			}
			break;
		case BT_INIT_SEQ_CMD2:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				LOG_BT("BT_INIT_SEQ_CMD2");
				MV_TX_ReadLocalBDAddress();
				MV_TX_ReadLocalClassOfDevice();
				ucBT_ProcessSeq = BT_INIT_SEQ_CMD3;
				usBT_ProcessSeq_Timer50msec = 50/50;
			}
			break;
		case BT_INIT_SEQ_CMD3:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				LOG_BT("BT_INIT_SEQ_CMD3");
//				MV_TX_SYS_StopSpkMicLoopBack();
				MV_TX_ReadLocalName();
			//	MV_TX_ReadPairedDevice();
				ucBT_InitOK = 1;

				ucBT_Process = BT_PRS_NONE;
				if  (ucBT_InitCold == 1)
				{
					ucBT_InitCold = 0;
				#ifndef  DEBUG
					MV_TX_ClearAllPairedDevice();
				#endif
				}
				else
				{
					usBT_AutoConnect_Timer100msec = 3000/100;
				}
			}
			break;
	}
}

unsigned char *BT_GetConnectedDeviceName(void)
{
	unsigned char i;

	for(i = 0; i < ucBT_DeviceCnt ; ++i)
	{
		if  (memcmp(&sBT_ConnectedDevice.ucDeviceAdr[0],&sBT_BDList[i].ucDeviceAdr[0],6) == 0)
		{
			break;
		}
	}

	return(&sBT_BDList[i].ucName[0]);
}

void BT_ConnectProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{	
		case BT_CON_SEQ_CHK:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY))
			{
				if  (memcmp(&sBT_ConnectedDevice.ucDeviceAdr[0],&sBT_BDList[ucBT_ConnectRequestIndex].ucDeviceAdr[0],6) == 0)
				{
					ucBT_ProcessSeq = 0;	// already con
					ucBT_Process = BT_PRS_NONE;
					MENU_SetID(MENU_BT_SEL_ID);
					LOG_BT("BT_CON_SEQ_CHK:CON");
				}
				else
				{
					if  (ucMV_PBAP_Status == MV_PBAP_DOWNLOADING)
					{
						MV_TX_PhoneBookAbort();
					}
					MV_TX_DisconnectAll();
					ucBT_ProcessSeq = BT_CON_SEQ_WAIT_DC;
					usBT_ProcessSeq_Timer50msec = 10000/50;
					LOG_BT("BT_CON_SEQ_CHK:DIS");
			//		if  (MENU_GetID() == MENU_BT_CONNETING_ID)
			//		{
			//			DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
			//		}
				}
			}
			else
			{
			//	if  (MENU_GetID() == MENU_BT_CONNETING_ID)
			//	{
			//		DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
			//	}
				ucBT_ProcessSeq = BT_CON_SEQ_REQ_C;
				usBT_ProcessSeq_Timer50msec = 10000/50;
				LOG_BT("BT_CON_SEQ_CHK:REQ");
			}
			break;
		case BT_CON_SEQ_WAIT_DC:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_ProcessSeq = BT_CON_SEQ_FAIL;// Fail con
				if  (MENU_GetID() == MENU_BT_CONNETING_ID)
				{
					MENU_SetID(MENU_NONE);
					DISP_SetTempDisplayMode(DISP_BT_NOCONNECT);
				}	
			}
			if  ((ucMV_HFS_Status == MV_HFS_CONNECTABLE)
				&& (ucMV_A2DP_Status == MV_A2DP_CONNECTABLE)
				&& (ucMV_AVRCP_Status == MV_AVRCP_READY)
				&& (ucMV_PBAP_Status == MV_PBAP_READY))
			{
				ucBT_ProcessSeq = BT_CON_SEQ_REQ_C;
				LOG_BT("BT_CON_SEQ_WAIT_DC:OK");
			}
			break;
	//	case BT_CON_SEQ_DISP_NAME:
	//		ucBT_ProcessSeq = BT_CON_SEQ_REQ_C;
		case BT_CON_SEQ_REQ_C:
			{
				MV_TX_ConnectAllToBDAddr(sBT_BDList[ucBT_ConnectRequestIndex]);
				memcpy(&sBT_ConnectedDevice.ucDeviceAdr[0], &sBT_BDList[ucBT_ConnectRequestIndex].ucDeviceAdr[0], 6);
			//	if  (MENU_GetID() == MENU_BT_CONNETING_ID)
			//	{
			//		DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
			//	}
				
				ucBT_ProcessSeq = BT_CON_SEQ_WAIT_C;
				usBT_ProcessSeq_Timer50msec = 10000/50;
				LOG_BT("BT_CON_SEQ_REQ_C");
			}
			break;
		case BT_CON_SEQ_WAIT_C:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				&& (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE))
			{
				BT_Update_BDSorting(ucBT_ConnectRequestIndex);
		//		if  (MENU_GetID() == MENU_BT_CONNETING_ID)
		//		{
		//			DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
		//		}
				ucBT_ProcessSeq = BT_CON_SEQ_DEVICE_NAME;
				usBT_ProcessSeq_Timer50msec = 3000/50;
				LOG_BT("BT_CON_SEQ_WAIT_C:OK");
				
			}
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_ProcessSeq = BT_CON_SEQ_FAIL;// Fail con
				LOG_BT("BT_CON_SEQ_WAIT_C:NG");
			}				
			break;
		case BT_CON_SEQ_DEVICE_NAME:
		//	if  (MENU_GetID() == MENU_BT_CONNETING_ID)
			{
				MENU_SetID(MENU_NONE);
				DISP_SetTempDisplayMode_6Sec(DISP_BT_CONNECT);
			}
			ucBT_Process = BT_PRS_NONE;
			ucBT_ProcessSeq = 0;
			LOG_BT("BT_CON_SEQ_DEVICE_NAME");
			break;			
		case BT_CON_SEQ_FAIL:
			ucBT_Process = BT_PRS_NONE;
			if  (MENU_GetID() == MENU_BT_CONNETING_ID)
			{
				MENU_SetID(MENU_NONE);
				DISP_SetTempDisplayMode(DISP_BT_NOCONNECT);
			}	
			LOG_BT("BT_CON_SEQ_FAIL");
			break;
	}
}

void BT_CommonProcess_SetCmd(void)
{
	if  (ucBT_Process == BT_PRS_ANSWER_CALL)
	{
		if  (ucMV_HF_Profile == MV_HFP_HSP)
		{
			MV_TX_HS_SendButtonPressReq();
		}
		else
		{
			MV_TX_HF_AnswerCall();
		}
		ucBT_RootCall = 1;
	}
	if  (ucBT_Process == BT_PRS_HANDUP_CALL)
	{
		if  (ucMV_HF_Profile == MV_HFP_HSP)
		{
			MV_TX_HS_SendButtonPressReq();
		}
		else
		{
			MV_TX_HF_HangUpCall();
		}
	}
	if  (ucBT_Process == BT_PRS_REJECT_CALL)
	{
		MV_TX_HF_RejectCall();
	}
	if  (ucBT_Process == BT_PRS_REDIAL)
	{
		BT_PBAP_SetDownload(2);
		if  (ucMV_HF_Profile == MV_HFP_HSP)
		{
			MV_TX_HS_SendButtonPressReq();
		}
		else
		{
			MV_TX_HF_LastNumberRedial();
		}
		ucBT_RootCall = 1;
	}
	if  (ucBT_Process == BT_PRS_DIALNUMBER)
	{
		BT_PBAP_SetDownload(2);
		MV_TX_HF_NumberDial(&ucBT_DialNumber[0],ucBT_DialNumberCnt);
		ucBT_RootCall = 1;
	}
	if  (ucBT_Process == BT_PRS_CALL_TRANS)
	{
		if  (ucMV_HF_Profile == MV_HFP_HSP)
		{
			MV_TX_HS_SendButtonPressReq();
		}
		else
		{
			MV_TX_HF_AudioTransfer();
		}
	}
		
	if  (ucBT_Process == BT_PRS_AVRCP_CMD)
	{
		switch  (ucBT_AVRCP_Cmd)
		{
			case BT_AVRCP_PLAY_PAUSE:	// Play Pause
				MV_TX_AV_PlayPause();
				break;
			case BT_AVRCP_STOP:	// Stop
				MV_TX_AV_Stop();
				break;
			case BT_AVRCP_NEXT:	// Next
				MV_TX_AV_Forward();
				break;
			case BT_AVRCP_PREVIOUS:	// Previous
				MV_TX_AV_Backward();
				break;
			case BT_AVRCP_FF:	// Fast-F
				MV_TX_AV_FastForward();
				break;
			case BT_AVRCP_REW:	// Fast-R
				MV_TX_AV_FastRewind();
				break;
		}
	}

	if  (ucBT_Process == BT_PRS_MIC_MUTE)
	{
		MV_TX_HF_MicMute();
	}
}

void BT_CommonProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{
		case BT_COM_SEQ_SETCMD:
			usBT_ProcessSeq_Timer50msec = 3000/50;
			BT_CommonProcess_SetCmd();
			ucBT_ProcessSeq = BT_COM_SEQ_WAIT_CFM;
			break;
		case BT_COM_SEQ_WAIT_CFM:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{			
				if  (ucBT_Process == BT_PRS_AVRCP_CMD)
				{
					ucBT_ProcessSeq = BT_PRS_AVRCP_CMD_REL_MUTE;
					usBT_ProcessSeq_Timer50msec = 1000/50;
				}
				else
				{
					ucBT_Process = BT_PRS_NONE;
				}
			}
			break;
		case BT_PRS_AVRCP_CMD_REL_MUTE:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_Process = BT_PRS_NONE;
				AUDIO_SetMute(0);
			}
			break;
	}
	
}

void BT_DisConnectProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{
		case BT_DIS_SEQ_PRE:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY))
			{
				if  (ucMV_HFS_Status > MV_HFS_CONNECTED)
				{
					if  (ucMV_HF_Profile == MV_HFP_HSP)
					{
						MV_TX_HS_SendButtonPressReq();
					}
					else
					{
						MV_TX_HF_AudioTransfer();
					}
				}
				if  (ucMV_PBAP_Status == MV_PBAP_DOWNLOADING)
				{
					MV_TX_PhoneBookAbort();
				}
				MV_TX_DisconnectAll();
				usBT_ProcessSeq_Timer50msec = 15000/50;
				ucBT_ProcessSeq = BT_DIS_SEQ_WAIT;
			}
			else
			{
				ucBT_Process = BT_PRS_NONE;
			}
			break;
		case BT_DIS_SEQ_WAIT:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_Process = BT_PRS_NONE;// Fail
			}
			if  ((ucMV_HFS_Status == MV_HFS_CONNECTABLE)
				&& (ucMV_A2DP_Status == MV_A2DP_CONNECTABLE)
				&& (ucMV_AVRCP_Status == MV_AVRCP_READY)
				&& (ucMV_PBAP_Status == MV_PBAP_READY))
			{
				// OK
				DISP_SetTempDisplayMode(DISP_BT_NOCONNECT);
			}
			break;
	}
}

void BT_BluetoothOnProcess(void)
{	
	switch  (ucBT_ProcessSeq)
	{
		case BT_ON_SEQ_ON:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE))
			{
				// OK
			}
			if  (ucMV_BTOnOff == 0x00)
			{
				MV_TX_BTOn();
			}
			ucBT_ProcessSeq = BT_ON_SEQ_WAIT;
			break;
		case BT_ON_SEQ_WAIT:
			if  (ucMV_BTOnOff == 0x01)	// On
			{
				ucBT_Process = BT_PRS_NONE;
				LOG_BT("<<BT ON>>");
			}
			break;
	}
}

void BT_BluetoothOffProcess(void)
{	
	switch  (ucBT_ProcessSeq)
	{
		case BT_OFF_SEQ_DISCON:
			if  ((ucMV_HFS_Status > MV_HFS_CONNECTABLE)
				|| (ucMV_A2DP_Status > MV_A2DP_CONNECTABLE)
				|| (ucMV_AVRCP_Status > MV_AVRCP_READY)
				|| (ucMV_PBAP_Status > MV_PBAP_READY))
			{
				if  (ucMV_PBAP_Status == MV_PBAP_DOWNLOADING)
				{
					MV_TX_PhoneBookAbort();
				}
				MV_TX_DisconnectAll();
				usBT_ProcessSeq_Timer50msec = 2000/50;
				ucBT_ProcessSeq = BT_OFF_SEQ_DISCON_WAIT;
			}
			else
			{
				ucBT_ProcessSeq = BT_OFF_SEQ_OFF;
			}
			break;
		case BT_OFF_SEQ_DISCON_WAIT:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_ProcessSeq = BT_OFF_SEQ_OFF;// Fail
			}
			if  ((ucMV_HFS_Status == MV_HFS_CONNECTABLE)
				&& (ucMV_A2DP_Status == MV_A2DP_CONNECTABLE)
				&& (ucMV_AVRCP_Status == MV_AVRCP_READY)
				&& (ucMV_PBAP_Status == MV_PBAP_READY))
			{
				ucBT_ProcessSeq = BT_OFF_SEQ_OFF;// Fail
			}
			break;
		case BT_OFF_SEQ_OFF:
			ucBT_Process = BT_PRS_NONE;
			PO_MOVON_RST= 0;
			ucMV_InitOk = 0;
			ucBT_InitOK = 0;
			ucMV_BTOnOff = 0;
			break;
	}
}

void BT_PhoneBookDownLoadProcess(void)
{
	switch  (ucBT_ProcessSeq)
	{
		case BT_PBAP_CHECK_CON:
			if  (ucMV_PBAP_Status == MV_PBAP_CONNECTED)
			{
				if  (ucPBAP_CurrentContents == PBAP_CONTENTS_PHONEBOOK)	
				{
				#ifdef  SERIAL_FLASH
					ucBT_ProcessSeq = BT_PBAP_CHECK_FLASH;
					SF_ErasePhoneData_Request();
				#else
					ucBT_ProcessSeq = BT_PBAP_REQUEST_DATA;
				#endif
				}
				else
				{
					ucBT_ProcessSeq = BT_PBAP_REQUEST_DATA;
				}
				LOG_BT("BT_PBAP_CHECK_CON:OK");				
			}
			else
			{
				MV_TX_PhoneBookConnect();
				LOG_BT("BT_PBAP_CHECK_CON:REQ");
				usBT_ProcessSeq_Timer50msec = 10000/50;
				ucBT_ProcessSeq = BT_PBAP_WAIT_CON;
			}
			break;
		case BT_PBAP_WAIT_CON:
			if  (ucMV_PBAP_Status == MV_PBAP_CONNECTED)
			{
				ucBT_ProcessSeq = BT_PBAP_CHECK_CON;
				usBT_ProcessSeq_Timer50msec = 0;
				LOG_BT("BT_PBAP_WAIT_CON:OK");
			}
			else
			{
				if  (usBT_ProcessSeq_Timer50msec == 0)
				{
					ucBT_Process = BT_PRS_NONE;
					LOG_BT("BT_PBAP_WAIT_CON:NG");
				}
			}
			break;
		case BT_PBAP_REQUEST_DATA:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				if  (ucPBAP_CurrentContents == PBAP_CONTENTS_CALLHISTORY)
				{
					MV_TX_PhoneBookCallListGet();
					LOG_BT("BT_PBAP_REQUEST_DATA:PB");
				}
			
				if  (ucPBAP_CurrentContents == PBAP_CONTENTS_PHONEBOOK)	
				{
					MV_TX_PhoneBookGet();
					LOG_BT("BT_PBAP_REQUEST_DATA:HIS");
				}
				ucBT_ProcessSeq = 0;
				ucBT_Process = BT_PRS_NONE;
			}
			break;
		case BT_PBAP_WAIT_START:
			if  (usBT_ProcessSeq_Timer50msec == 0)
			{
				ucBT_Process = BT_PRS_NONE;
				LOG_BT("BT_PBAP_WAIT_START");
			}
			break;
	#ifdef  SERIAL_FLASH
		case BT_PBAP_CHECK_FLASH:
			if  (SF_CommunicationActived() == 0)
			{
				ucBT_ProcessSeq = BT_PBAP_WRITE_DEVICE_ADDR;
				LOG_BT("BT_PBAP_CHECK_FLASH");
			}
			break;
		case BT_PBAP_WRITE_DEVICE_ADDR:
			SF_WriteBluetoothDeviceAddress();
			ucBT_ProcessSeq = BT_PBAP_WRITE_DEVICE_ADDR_WAIT;
			break;
		case BT_PBAP_WRITE_DEVICE_ADDR_WAIT:
			if  (SF_CommunicationActived() == 0)
			{
				ucBT_ProcessSeq = BT_PBAP_REQUEST_DATA;
			#ifdef  SERIAL_FLASH
				PBAP_DataInit();
			#endif
				LOG_BT("BT_PBAP_CHECK_FLASH");
			}
			break;
	#endif
	}
}

void BT_AutoConnectionStart(void)
{
	usBT_AutoConnect_Timer100msec = 0;
	ucBT_AutoConnection = 1;
}

void BT_AutoConnectionStop(void)
{
	ucBT_AutoConnection = 0;
}

void BT_AutoConnectionProcess(void)
{
	if  (ucBT_InitOK == 0)
	{
		return;
	}
	if  (ucBT_AutoConnection == 0)
	{
		return;
	}
	if  (usBT_AutoConnect_Timer100msec != 0)
	{
		return;
	}
	LOG_BT("BT Auto");
	BT_ConnectRequest(0);
//	MENU_SetID(MENU_BT_CONNETING_ID);
	usBT_AutoConnect_Timer100msec = 60000/100;
}
void BT_FunctionStop(void)
{
	ucBT_PBAP_Download_Active = 0;
	ucBT_InitOK = 0;
	ucMV_InitOk = 0;
	memset(&ucBT_CmdBuf[0], 0x00, BT_CMD_CNT);
	ucBT_Cmd_rPtr = 0;
	ucBT_Cmd_wPtr = 0;
	ucBT_ProcessSeq = 0;
	PO_MOVON_RST = 0;
	BT_SetDBAddress(BT_DB_ADR_NORMAL);
}

void BT_InitializeStart(void)
{
	ucBT_ProcessSeq = BT_INIT_SEQ_RESET0;
//	BT_SetProcess(BT_PRS_INITIALIZE);
}

unsigned char BT_CurrentProcess_Connect(void)
{
	if  (ucBT_Process == BT_PRS_CONNECT)
	{
		return(1);
	}

	return(0);
}

void BT_PBAP_DownloadStart(void)
{
	if  (ucBT_PBAP_Download_Active == 0)
	{
		return;
	}
	if  (ucBT_PBAP_Download_Timer50msec == 0)
	{
		ucBT_PBAP_Download_Active = 0;
		if  (ucPBAP_CurrentContents == PBAP_CONTENTS_CALLHISTORY)
		{
			BT_SetProcess(BT_PRS_CALLHISTORY_DOWNLOAD);
		}
		else
		{
			BT_SetProcess(BT_PRS_PABP_DOWNLOAD);
		}
	}
}

void BT_ProcessMain(void)
{
	if  ((ucBT_Function_Active == 1) && (ucBT_InitOK == 0))
	{
		BT_InitializeProcess();
		return;
	}
	switch  (ucBT_Process)
	{
		case BT_PRS_NONE:
			if  (ucBT_Cmd_rPtr == ucBT_Cmd_wPtr)
			{
				BT_AutoConnectionProcess();
				BT_PBAP_DownloadStart();
			}
			else
			{
				ucBT_Process = BT_GetProcess();
				ucBT_ProcessSeq = 0;
			}
			break;
//		case BT_PRS_INITIALIZE:
//			BT_InitializeProcess();
//			break;
		case BT_PRS_PAIRING:
			BT_PairProcess();
			break;
		case BT_PRS_DELETE:
			BT_DeleteProcess();
			break;
		case BT_PRS_CONNECT:
			BT_ConnectProcess();
			break;
		case BT_PRS_DISCONNECT:
			BT_DisConnectProcess();
			break;
		case BT_PRS_ON:
			BT_BluetoothOnProcess();
			break;
		case BT_PRS_OFF:
			BT_BluetoothOffProcess();
			break;
		case BT_PRS_CALLHISTORY_DOWNLOAD:
		case BT_PRS_PABP_DOWNLOAD:
			BT_PhoneBookDownLoadProcess();
			break;
		case BT_PRS_LOOPBACK:
			MV_TX_SYS_StartSpkMicLoopBack();
			FUNC_FunctionChangeStart(FUNC_LPB, FUNC_CHANGE_SEQ0);
			MV_TX_HF_ReadMICVolume_Req();
			ucBT_ProcessSeq = 0;
			ucBT_Process = BT_PRS_NONE;
			LOG_BT("BT_PRS_LOOPBACK");
			break;
		default:
			BT_CommonProcess();
			break;
	}
}

/*//////////////////////////////////////////////////////////////////////////
Title : void BT_BluetoothVolume(unsigned char ucDes)
ucDes
	0 : Down
	1 : Up
ucOption
	0 : None Change Audio Output
	1 : Change Audio
//////////////////////////////////////////////////////////////////////////*/
void BT_BluetoothVolume(unsigned char ucDes, unsigned char ucOption)
{
	if  (ucDes == 0)
	{
		if  (ucBT_Volume != BT_VOL_MIN)
		{
			--ucBT_Volume;
		}		
	}
	else
	{
		if  (++ucBT_Volume > BT_VOL_MAX)
		{
			ucBT_Volume = BT_VOL_MAX;
		}
	}
	#ifdef  HERO
	if  ((ucOption == 1) && (ucBT_Volume != 0))
	{
		LOG_SYS("BT_BluetoothVolume:%d",ucBT_Volume);
		HERO_SetVolume(ucBT_Volume);
	}
	#endif
}

void BT_SetVolume(void)
{
	#ifdef  HERO
	if  (ucBT_Volume != 0)
	{
		LOG_SYS("BT_BluetoothVolume:%d",ucBT_Volume);
		HERO_SetVolume(ucBT_Volume);
	}
	#endif
}

void BT_BluetoothVolumeInit(void)
{

	if  (ucBT_Volume > BT_VOL_DEFAULT)
	{
		ucBT_Volume = BT_VOL_DEFAULT;
	}
}
/*/////////////////////////////////////////////////////////////////////////////
Title : void BT_BluetoothOnOff(unsigned char ucDes)
ucDes
	0 : Reguest Off
	1 : Request On
//////////////////////////////////////////////////////////////////////////////*/
void BT_BluetoothOnOff(unsigned char ucDes)
{
	if  (ucDes == 1)
	{
		if  (ucMV_BTOnOff == 0x00)
		{
		//	BT_SetProcess(BT_PRS_ON);
			ucBT_Function_Active = 1;
			ucBT_ProcessSeq = 0;
			ucBT_Process = 0;
	//		BT_AutoConnectionStart();
			BT_InitializeStart();
		}
	}
	else
	{
		if  (ucMV_BTOnOff == 0x01)
		{
			BT_SetProcess(BT_PRS_OFF);
		}
	}
}

/*/////////////////////////////////////////////////////////////////////////////
Title : void BT_ConnectRequest(unsigned char ucOrder)
ucMode : Index type
	0 : BD List Index
	1 : Order
/////////////////////////////////////////////////////////////////////////////*/
unsigned char BT_ConnectRequest(unsigned char ucOrder)
{
	if  (ucBT_DeviceCnt == 0)
	{
		return(0);
	}
	
	ucBT_ConnectRequestIndex = BT_GetBDIndexByOrder(ucOrder);
	if  (BT_ConnectStatus() == 1)
	{
		if  (memcmp(&sBT_ConnectedDevice.ucDeviceAdr[0],&sBT_BDList[ucBT_ConnectRequestIndex].ucDeviceAdr[0],6) == 0)
		{
			MENU_SetID(MENU_BT_SEL_ID);
			return(0);
		}
	}
	BT_SetProcess(BT_PRS_CONNECT);
	return(1);
}

void BT_PairingRequest(void)
{
	BT_SetProcess(BT_PRS_PAIRING);	
	usBT_Pair_Timer50msec = 60000/50;
}

unsigned char BT_PairingCancel(void)
{
	if  (BT_PRS_PAIRING == ucBT_Process)
	{
		if  (MENU_GetID() == MENU_BT_PAIR_ACTIVE_ID)
		{
			MENU_SetID(MENU_NONE);
		}
		ucBT_Process = BT_PRS_NONE;
		usBT_Pair_Timer50msec = 0x00;
		MV_TX_StopPairing();	
		return(1);
	}
	return(0);
}

unsigned char BT_ConnectingCancel(void)
{
	if  (BT_PRS_CONNECT == ucBT_Process)
	{
		if  (MENU_GetID() == MENU_BT_CONNETING_ID)
		{
			MENU_SetID(MENU_NONE);
		}
		ucBT_Process = BT_PRS_NONE;
		usBT_Pair_Timer50msec = 0x00;
		return(1);
	}
	return(0);
}

void BT_DisconnectRequest(void)
{
	BT_SetProcess(BT_PRS_DISCONNECT);
}

void BT_DeleteRequest(unsigned char ucOrder)
{
	ucBT_DeleteRequestIndex = BT_GetBDIndexByOrder(ucOrder);
	BT_SetProcess(BT_PRS_DELETE);
}

void BT_DialNumberRequest(void)
{
	BT_SetProcess(BT_PRS_DIALNUMBER);
}

void BT_AVRCP_Command(BT_AVRCP ucCmd)
{
	if  (ucMV_AVRCP_Status == MV_AVRCP_CONNECTED)
	{
		ucBT_AVRCP_Cmd = ucCmd;
		BT_SetProcess(BT_PRS_AVRCP_CMD);
	}
}

void BT_CallHistoryDownLoad_Request(void)
{
//	BT_SetProcess(BT_PRS_CALLHISTORY_DOWNLOAD);
	PBAP_SelectMode(PBAP_CONTENTS_CALLHISTORY);
	PBAP_InitBlockData();
	ucPBAP_CallHistoryCnt = 0;
	MV_PB_SkipInit();
}

void BT_PhoneBookDownLoad_Request(void)
{
//	BT_SetProcess(BT_PRS_PABP_DOWNLOAD);
	PBAP_SelectMode(PBAP_CONTENTS_PHONEBOOK);
	PBAP_InitBlockData();
	MV_PB_SkipInit();
}

void BT_CallTransfer(void)
{
	if  (ucMV_HF_AudioConnect == HF_AUDIO_PHONE)
	{
		return;
	}
	if	(ucMV_HFS_Status > MV_HFS_INCOMING)
	{
		BT_SetProcess(BT_PRS_CALL_TRANS);
	}
}

/*/////////////////////////////////////////////////////////////////////////////
Title : void BT_PhoneBookCtrl(unsigned char ucDes)
ucDes
	0 : Phone Book Request Download
	1 : Call List Download
	2 : Abort
	3 : Delete
/////////////////////////////////////////////////////////////////////////////*/
void BT_PBAP_SetDownload(unsigned char ucDes)
{
#if 1
	switch  (ucDes)
	{
		case 0:
			BT_PhoneBookDownLoad_Request();
			ucBT_PBAP_Download_Timer50msec = 3000/50;
			ucBT_PBAP_Download_Active = 1;
			break;
		case 1:
			BT_CallHistoryDownLoad_Request();
			ucBT_PBAP_Download_Timer50msec = 3000/50;
			ucBT_PBAP_Download_Active = 1;
			break;
		case 2:
	//		if  ((ucBT_Process == BT_PRS_CALLHISTORY_DOWNLOAD) || (ucBT_Process == BT_PRS_PABP_DOWNLOAD))
			if  (ucMV_PBAP_Status == MV_PBAP_DOWNLOADING)
			{
				MV_TX_PhoneBookAbort();
				ucBT_Process = BT_PRS_NONE;
			}
			break;
		case 3:
		#ifdef  SERIAL_FLASH
			SF_ErasePhoneData_Request();
		#endif
			break;
	}
#endif
}
#endif

